import 'dart:convert';
import 'dart:io';

import '../utils/convert_utils.dart';

class Config {
  Map<String, List<String>?>? _fileExtensions;
  Map<String, List<String>?>? get fileExtensions => _fileExtensions;
  void readFromFile() async {
    String appPath = Directory.current.path;
    String configPath = "$appPath\\config.json";
    File configFile = File(configPath);
    bool exists = await configFile.exists();
    if (exists == true) {
      String jsonString = await configFile.readAsString();
      final config = json.decode(jsonString);
      _parseFileExtensions(config);
    }
  }

  void _parseFileExtensions(Map<String, dynamic> config) {
    final fileExtensionsProp = config["fileExtensions"];
    if (fileExtensionsProp != null) {
      _fileExtensions = {};
      for (final entry in fileExtensionsProp.entries) {
        _fileExtensions![entry.key] = dynamicList2StringList(entry.value);
      }

      List<String>? bookAndVideoExtensions = _fileExtensions!["book"] ?? [];

      List<String>? videoExtensions = _fileExtensions!["video"];
      if (videoExtensions != null && videoExtensions.isNotEmpty == true) {
        bookAndVideoExtensions.addAll(videoExtensions);
      }

      _fileExtensions!["videoAndBook"] =
          bookAndVideoExtensions.isEmpty ? null : bookAndVideoExtensions;
    }
  }
}
